﻿using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HuiZe.education.View
{
    /// <summary>
    /// StudentView.xaml 的交互逻辑
    /// </summary>
    public partial class StudentView : UserControl
    {
        StudentViewModel studentViewModel = null;
        public StudentView()
        {
            InitializeComponent();
            Loaded += StudentView_Loaded;

        }



        public void root_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var a = e;

        }

        //RadioButton_Click

        public bool ShowDialogView(string content)
        {
            ShowDialogView4 showDialogView = new ShowDialogView4() { ShowDialogContent = content };
            if (showDialogView.ShowDialog() == true)
            {
                return true;

            }
            else
            {
                return false;

            }
        }
        public bool ShowDialogView2(string content)
        {
            ShowDialogView showDialogView = new ShowDialogView() { ShowDialogContent = content };
            if (showDialogView.ShowDialog() == true)
            {
                return true;

            }
            else
            {
                return false;

            }
        }
        private void StudentView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                studentViewModel = DataContext as StudentViewModel;

                studentViewModel.studentView = this;
                UpdatePager();
            }
            catch (Exception ex)
            { 
            }
        }

        public void UpdatePager()
        {

            pageManage.ItemSourceCount = studentViewModel.pageCount;
            pageManage.UpdataPages();
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {

            if (HuiZeeducation.Instance.UserInfo.auth != 1)
            {
                ShowDialogView2("当前账户暂无操作权限!");
                return;

            }
            if (GridStudents.SelectedItem != null)
            {
                StudentModel student = (StudentModel)GridStudents.SelectedItem;

                studentViewModel.StudentId = student.id;
                studentNameTxt.Text = student.StuName;
                if (student.studentSex == "男")
                {
                    studentSexMan.IsChecked = true;
                }
                else
                {
                    studentSexWomen.IsChecked = true;
                }
                comClass.Text = student.StuClass;
                stuNo.Text = student.student_no;
                ComStatus.Text = student.StrStuStatus == "毕业" ? "离校" : "";
                parentName.Text = student.ParentName;
                parentPhone.Text = student.ParentMobile;

                eidtTitle.Text = "修改";
                bg_addStu.Visibility = Visibility.Visible;


            }



        }

        private void addStu_Click(object sender, RoutedEventArgs e)
        {

            //if (HuiZeeducation.Instance.UserInfo.auth != 1)
            //{
            //    ShowDialogView2("当前账户暂无操作权限!");
            //    return;

            //}
            studentViewModel.StudentId = 0;
            eidtTitle.Text = "添加";
            studentNameTxt.Text = "";

            studentSexMan.IsChecked = true;

            comClass.Text = "";
            stuNo.Text = "";
            ComStatus.Text = "";
            parentName.Text = "";
            parentPhone.Text = "";

            bg_addStu.Visibility = Visibility.Visible;
        }

        private void exitAddStu_Click(object sender, RoutedEventArgs e)
        {
            bg_addStu.Visibility = Visibility.Collapsed;
        }




        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {

            if (HuiZeeducation.Instance.UserInfo.auth != 1)
            {
                ShowDialogView2("当前账户暂无操作权限!");
                return;

            }
            string ids = "";
            string names = "";

            //System.Collections.IList selectedItems = GridStudents.SelectedItems;

            if (selectedItems2 != null)
            {

                for (int j = 0; j < selectedItems2.Count; j++)
                {
                    StudentModel student = (StudentModel)selectedItems2[j];
                    int id = student.id;
                    if (j == selectedItems2.Count - 1)
                    {
                        ids += id.ToString();
                        names += student.StuName;
                    }
                    else
                    {
                        names += student.StuName + ",";
                        ids += id.ToString() + ",";

                    }
                }

            }

            if (!string.IsNullOrWhiteSpace(ids))
            {
                StudentModel student = (StudentModel)GridStudents.SelectedItem;
                int Id = student.id;//获取选中行的name列内容
                string studentName = student.StuName;

                //MessageBoxResult result = MessageBox.Show("确定要删除学生"+ studentName + "？", "提示", MessageBoxButton.YesNo);//弹出删除对话框

                bool result = ShowDialogView("确定要删除学生" + names + "？");
                switch (result)
                {
                    case true:
                        studentViewModel.DeleteStudent(ids); ;//调用函数，删除数据库
                        //DRV.Delete();//删除行
                        //MessageBox.Show("删除成功！");
                        break;
                    case false:
                        break;
                }
            }
        }



        //public  void DelUserData(string Id) 
        //{

        //    if (string.IsNullOrEmpty(Id))
        //    {
        //        //ErrorMsg.LoginMobileError = "学生姓名不能为空";
        //        return;
        //    }

        //    string data = JsonHelper.ToJson(studentModel);
        //    var result = Request<ReqStudentModel>("/api/student/delStudent", data, "删除学生");
        //    if (result.Item1)
        //    {
        //        ShowDialogView("添加成功!");
        //        GetStudentList();

        //    }
        //    else
        //    {
        //        if (result.Item2 != null)
        //            ShowDialogView(result.Item2.msg);
        //    }

        //}

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void YCButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (HuiZeeducation.Instance.UserInfo.auth != 1)
            {
                ShowDialogView2("当前账户暂无操作权限!");
                return;

            }

            studentViewModel.DownLoadTemp();


        }
        /// <summary>
        /// 一键导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YCButton_Click_3(object sender, RoutedEventArgs e)
        {
            if (HuiZeeducation.Instance.UserInfo.auth != 1)
            {
                ShowDialogView2("当前账户暂无操作权限!");
                return;

            }
            studentViewModel.ImportFile();
        }
        /// <summary>
        /// 一键导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YCButton_Click_4(object sender, RoutedEventArgs e)
        {
            studentViewModel.ExportFile(2, "");
        }
        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YCButton_Click_5(object sender, RoutedEventArgs e)
        {
            string ids = "";

            //System.Collections.IList selectedItems = GridStudents.SelectedItems;

            if (selectedItems2 != null)
            {

                for (int j = 0; j < selectedItems2.Count; j++)
                {
                    StudentModel student = (StudentModel)selectedItems2[j];
                    int id = student.id;
                    if (j == selectedItems2.Count - 1)
                    {
                        ids += id.ToString();
                    }
                    else
                    {
                        ids += id.ToString() + ",";

                    }
                }

            }

            studentViewModel.ExportFile(1, ids);

        }



        private void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            {
                CustomRadioButton button = e.OriginalSource as CustomRadioButton;
                string index = button.Content.ToString();
                if (index == ">")
                {
                    //index = pageManage.Content.ToString();
                    index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
                }
                if (index == "<")
                {
                    index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
                }
                if (index == "首页")
                {
                    index = "1";
                }
                if (index == "尾页")
                {
                    index = pageManage.ItemSourceCount.ToString();
                }


                studentViewModel.GetStudentList(Convert.ToInt32(index));
            }
        }

        private void YCWateTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("搜索"))
                studentViewModel.GetStudentList(1);


        }

        private void YCButton_Click_6(object sender, RoutedEventArgs e)
        {
            if (studentSexMan.IsChecked == true)
            {
                studentViewModel.studentModel.StuSex = 1;
            }
            else
            {
                studentViewModel.studentModel.StuSex = 2;
            }
            studentViewModel.AddStudent();
            bg_addStu.Visibility = Visibility.Collapsed;
        }

        //System.Collections.IList selectedItems2=null;

        List<StudentModel> selectedItems2 = new List<StudentModel>();
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            if (chk.IsChecked == true)
            {
                var source = GridStudents.ItemsSource;

                var selectedItem = GridStudents.SelectedItem as StudentModel;
                selectedItems2.Add(selectedItem);

            }
            else
            {
                var selectedItem = GridStudents.SelectedItem as StudentModel;
                selectedItems2.Remove(selectedItem);

            }
        }

        private void YCButton_Click_7(object sender, RoutedEventArgs e)
        {
            bg_addStu.Visibility = Visibility.Collapsed;
        }

        private void YCButton_Click_8(object sender, RoutedEventArgs e)
        {
            if (HuiZeeducation.Instance.UserInfo.auth != 1)
            {
                ShowDialogView2("当前账户暂无操作权限!");
                return;

            }
            string ids = "";
            string names = "";

            var selectedItem = GridStudents.SelectedItem;

            if (selectedItem != null)
            {
                StudentModel student = (StudentModel)selectedItem;
                int id = student.id;

                ids += id.ToString();

            }

            if (!string.IsNullOrWhiteSpace(ids))
            {
                StudentModel student = (StudentModel)GridStudents.SelectedItem;
                int Id = student.id;//获取选中行的name列内容
                string studentName = student.StuName;

                //MessageBoxResult result = MessageBox.Show("确定要删除学生"+ studentName + "？", "提示", MessageBoxButton.YesNo);//弹出删除对话框

                bool result = ShowDialogView("确定要删除学生" + names + "？");
                switch (result)
                {
                    case true:
                        studentViewModel.DeleteStudent(ids); ;//调用函数，删除数据库
                        //DRV.Delete();//删除行
                        //MessageBox.Show("删除成功！");
                        break;
                    case false:
                        break;
                }
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CommonModel student = ClassCom.SelectedItem as CommonModel;
            if (student != null)
            {
                studentViewModel.QuerystudentModel.StuClass = student.Title;
                studentViewModel.GetStudentList(1);
                ClassCom.UpdateComboxStatus();
            }
        }



        private void ClassCom_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //ComboBox combo = sender as ComboBox;

            CustomButton customButton = e.OriginalSource as CustomButton;
            if (customButton != null)
            {
                if (customButton.Name == "deleteButton")
                {

                    studentViewModel.QuerystudentModel.StuClass = "";
                    studentViewModel.GetStudentList(1);
                }


            }
            else
            {
                ClassCom.OpenCombox();
            }
        }

        private void ClassCom_LostFocus(object sender, RoutedEventArgs e)
        {
            ClassCom.UpdateComboxStatus();

        }

        private void ClassCom_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void ClassCom_MouseUp(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
